# """
# Author: Broin
# Date: 2023-06-29 17:57:59
# LastEditTime: 2023-06-30 16:59:44
# LastEditors: your name
# Description:
# FilePath: /zhoujin/audioStreamCrypto/POSTtest.py
# MADE BY BROIN
# """

import requests
import base64


# 测试加密
def test_encrypt():
    url = "http://localhost:5000/encrypt"  # 根据实际情况修改 URL
    # audio_data = b"audio data"  # 音频数据
    with open("./audio.mp3", "rb") as f:
        audio_data = f.read()

    proxies = {
        "http": None,
        "https": None
    }
    response = requests.post(url, data=audio_data, proxies=proxies)
    if response.status_code == 200:
        encrypted_data = response.json()["encrypted_data"]
        # print(encrypted_data)
        return encrypted_data
        # Convert pointer to bytes and encode as Base64
    else:
        print("Error during encryption:", response.json())


# 测试解密
def test_decrypt(encrypted_data):
    url = "http://localhost:5000/decrypt"  # 根据实际情况修改 URL
    # encrypted_data = b"encrypted data"  # 密文音频流
    proxies = {
        "http": None,
        "https": None
    }
    response = requests.post(url, data=encrypted_data, proxies = proxies)
    if response.status_code == 200:
        decrypted_data = response.json()["decrypted_data"]
        # print("Decrypted data:", decrypted_data)
        return decrypted_data
    else:
        print("Error during decryption:", response.json())


# 运行测试
ret = test_encrypt()
#print("type of encrypted is : ", type(encrypted))
# print("ret", encrypted)
# base64_ret = ret.encode(encoding="utf-8")
# encrypted = base64.b64decode(base64_ret)
encrypted = ret
print("len of encrypted(byte):", len(encrypted))
decrypted = test_decrypt(encrypted)
de_base64_ret = decrypted.encode(encoding="utf-8")
de = base64.b64decode(de_base64_ret)
with open("./deaudio.mp3", "wb") as f:
    f.write(de)
